package org.easyframework.core.entity;

import java.io.Serializable;

/**
 * service层处理结果通用类，多结果返回时使用
 * 
 * @author: daiw
 * @date: 2018年10月18日 下午5:00:00
 */
public class ProcessResult<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6374136557431276784L;
	public final static boolean SUCCESS = true;
	public final static boolean FAILED = false;

	/**
	 * 是否处理成功
	 */
	private boolean isSuccess;
	/**
	 * 返回结果
	 */
	private T data;
	private int code;
	private String message;

	public boolean isSuccess() {
		return isSuccess;
	}

	public boolean isFailed() {
		return !isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the code
	 */
	public int getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(int code) {
		this.code = code;
	}

	public static <T> ProcessResult<T> getInst(boolean isSuccess) {
		ProcessResult<T> result = new ProcessResult<T>();
		result.setSuccess(isSuccess);
		return result;
	}

	public static <T> ProcessResult<T> getInstSuccess() {
		return getInst(ProcessResult.SUCCESS);
	}

	public static <T> ProcessResult<T> getInstSuccess(T data) {
		return getInst(ProcessResult.SUCCESS, data);
	}

	public static <T> ProcessResult<T> getInstFailed() {
		return getInst(ProcessResult.FAILED);
	}

	public static <T> ProcessResult<T> getInstFailed(String failedMessage) {
		return getInst(ProcessResult.FAILED, failedMessage);
	}

	public static <T> ProcessResult<T> getInst(boolean isSuccess, T object) {
		ProcessResult<T> inst = getInst(isSuccess);
		inst.setData(object);
		return inst;
	}

	public static <T> ProcessResult<T> getInst(boolean isSuccess, T object, String message) {
		ProcessResult<T> inst = getInst(isSuccess, object);
		inst.setMessage(message);
		return inst;
	}

	public static <T> ProcessResult<T> getInst(boolean isSuccess, String message) {
		ProcessResult<T> inst = getInst(isSuccess);
		inst.setMessage(message);
		return inst;
	}

	public static <T> ProcessResult<T> getInst(int code) {
		ProcessResult<T> result = new ProcessResult<T>();
		result.setCode(code);
		return result;
	}

	public static <T> ProcessResult<T> getInst(int code, String message) {
		ProcessResult<T> inst = getInst(code);
		inst.setMessage(message);
		return inst;
	}

	public static <T> ProcessResult<T> getInst(int code, T object, String message) {
		ProcessResult<T> inst = getInst(code);
		inst.setMessage(message);
		inst.setData(object);
		return inst;
	}
}
